//
//  TabBarViewController.m
//  Product_NEWS
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "TabBarViewController.h"
#import "NavigationController.h"
#import "NewsViewController.h"
#import "PictureViewController.h"
#import "VideoViewController.h"
#import "UserViewController.h"
#import "TabbarView.h"
#import "ReadViewController.h"
@interface TabBarViewController ()<TabbarViewDelegate>

@property (nonatomic , retain) TabbarView *tabbar;
@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTabbar];
    
    [self initControl];

    
}
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    self.view.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    for (UIView *child in self.tabBar.subviews) {
        if([child isKindOfClass:[UIControl class]])
        {
            [child removeFromSuperview];
        }
    }
}


-(void)initTabbar
{
    TabbarView *tabbar = [[TabbarView alloc]init];
    tabbar.frame = self.tabBar.bounds;
    tabbar.delegate = self;
    [self.tabBar addSubview:tabbar];
    self.tabbar = tabbar;
//       [UITabBar appearance].translucent = NO;
   
}

-(void)tabbar:(TabbarView *)tabbar didselectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
}

-(void)initControl
{
    NewsViewController  *new = [[NewsViewController alloc]init];
    [self setupChildViewController:new title:@"新闻" imageName:@"new" selectedImage:@"new1"];
    
    ReadViewController *read = [[ReadViewController alloc]init];
    [self setupChildViewController:read title:@"阅读" imageName:@"read" selectedImage:@"read1"];
    
    
    PictureViewController *picture = [[PictureViewController alloc]init];
    [self setupChildViewController:picture title:@"图片" imageName:@"picture" selectedImage:@"picture1"];
    
    VideoViewController *video = [[VideoViewController alloc]init];
    [self setupChildViewController:video title:@"视频" imageName:@"video" selectedImage:@"video1"];
    
    
    UserViewController *me = [[UserViewController alloc]init];
    [self setupChildViewController:me title:@"我的" imageName:@"user" selectedImage:@"user1"];
    
}


-(void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
    
    //设置控制器属性
    childVc.title = title;
    
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //包装一个导航控制器
    NavigationController *nav = [[NavigationController alloc]initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    [self.tabbar addTabBarButtonWithItem:childVc.tabBarItem];
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
